package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.utils.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class ItemServiceImpl implements ItemService {

    @Resource
    private TbItemCatMapper itemCatMapper;



    //分页展示数据
    @Override
    public PageBean<TbItemCat> findAll(Long parentId,Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        TbItemCatExample itemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = itemCatExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = itemCatMapper.selectByExample(itemCatExample);
        PageInfo<TbItemCat> pageInfo = new PageInfo<>(list);
        return new PageBean<>(pageNum,pageSize,list,pageInfo.getTotal(),pageInfo.getPages());
    }

    //获取一级分类
    @Override
    public List<TbItemCat> getOne(Long id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        return itemCatMapper.selectByExample(example);
    }

    //增加分类
    @Override
    public void addItem(TbItemCat itemCat) {
        itemCatMapper.insert(itemCat);
    }

    //回显
    @Override
    public TbItemCat editItem(Long id) {
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(id);
        if (itemCat.getParentId() != 0) {
            TbItemCat itemCat1 = itemCatMapper.selectByPrimaryKey(itemCat.getParentId());
            itemCat.setParentIdOne(itemCat1.getParentId());
        }
        return itemCat;
    }

    //修改方法
    @Override
    public void updateItem(TbItemCat itemCat) {
        itemCatMapper.updateByPrimaryKeySelective(itemCat);
    }

    //删除方法
    @Override
    public void deleteItem(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        criteria.andIdIn(list);
        itemCatMapper.deleteByExample(tbItemCatExample);
    }
}
